<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use app\common\model\AttachmentModel;
use app\common\model\AttachmentNavModel;
use think\Db;

/**
 * 新附件管理控制器
 * Class File
 * @package app\admin\controller\general
 */
class File extends Backend
{

    /**
     * Attachment模型对象
     * @var \app\common\model\AttachmentModel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AttachmentModel();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 获取列表
     * @return mixed|string|\think\response\Json
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if (request()->isAjax()) {
            $page      = input('param.page/d', 1);
            $rows      = input('param.rows/d', 20);
            $key       = input('param.keyword', '');
            $type      = input('param.type', '');
            $category  = input('param.category', '');
            $nav       = input('param.nav/a', '');
            $mimetype  = input('param.mimetype', '');
            $startTime = input('param.start_time', '');
            $endTime   = input('param.end_time', '');
            $order     = input('param.order', '');
            $result    = $this->model->getLists([
                'keyword'    => $key,
                'type'       => $type,
                'category'   => $category,
                'nav'        => $nav,
                'mimetype'   => $mimetype,
                'start_time' => $startTime,
                'end_time'   => $endTime,
                'order'      => $order,
            ], $rows, $page);

            $attachmentNavModel = new AttachmentNavModel();
            $result['navList']  = $attachmentNavModel->getNavList();

            return json($result);
        }

        return $this->fetch();
    }

    /**
     * 归类
     * @return \think\response\Json
     */
    public function nav()
    {
        $data = input('post.');
        if ($this->model->nav($data)) {
            return json([
                'code' => 200,
                'msg'  => '归类成功'
            ]);
        } else {
            return json([
                'code' => 400,
                'msg'  => '归类失败'
            ]);
        }
    }
}
